import isArrayLike from '../util/isArrayLike'
import optimizeCallback from '../function/optimizeCallback'

function each(obj, iteratee, context) {
  iteratee = optimizeCallback(iteratee, context, 3)
  const keys = !isArrayLike(obj) && Object.keys(obj)
  const length = (keys || obj).length

  for (let i = 0; i < length; ++i) {
    const currentKey = keys ? keys[i] : i
    iteratee(obj[currentKey], currentKey, obj)
  }
}

export default each
